package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {

        return teachplanMapper.findTeachplanTree(courseId);
    }

    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if(id!=null){
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else{
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(),
                    teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count+1);
            BeanUtils.copyProperties(teachplanDto,teachplanNew);
            teachplanMapper.insert(teachplanNew);
        }
    }

    @Transactional
    @Override
    public void deleteTeachplan(Long teachPlanId) {
//        删除第一级别的章时要求章下边没有小节方可删除。
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        if (teachplan.getGrade()==1){
            LambdaQueryWrapper<Teachplan> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Teachplan::getCourseId,teachplan.getCourseId());
            wrapper1.eq(Teachplan::getParentid,teachPlanId);
            wrapper1.eq(Teachplan::getGrade,2);
            Integer count1 = teachplanMapper.selectCount(wrapper1);
            if (count1!=0){
                XueChengPlusException.cast("当前章节下有小节需要先删除小节后才能操作");
            }else {
                teachplanMapper.deleteById(teachPlanId);
            }
        } else {
//        删除第二级别的小节的同时需要将其它关联的视频信息也删除。
            LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TeachplanMedia::getTeachplanId,teachplan.getId());
            int delete = teachplanMediaMapper.delete(wrapper);
            teachplanMapper.deleteById(teachPlanId);
        }

    }

    @Override
    public void teachplanMoveup(Long teachPlanId) {
//        下面
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        Integer orderby = teachplan.getOrderby();
        if (orderby==1){
            XueChengPlusException.cast("当前是最顶端");
        }
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        wrapper.eq(Teachplan::getOrderby,orderby-1);
//        上面
        Teachplan teachplan1 = teachplanMapper.selectOne(wrapper);
        teachplan.setOrderby(orderby-1);
        teachplan1.setOrderby(orderby);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplan1);
    }

    @Override
    public void teachplanMovedown(Long teachPlanId) {
        //        下面
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        Integer orderby = teachplan.getOrderby();
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getParentid,teachplan.getParentid());
        wrapper.eq(Teachplan::getOrderby,orderby+1);
//        上面
        Teachplan teachplan1 = teachplanMapper.selectOne(wrapper);
        if (teachplan1 == null) {
            XueChengPlusException.cast("当前是最低端");
        }
        teachplan.setOrderby(orderby+1);
        teachplan1.setOrderby(orderby);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplan1);
    }

    @Override
    public TeachplanMedia  associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();

        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,teachplanId));

        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

    @Override
    public void deleteMedia(Long teachPlanId, String mediaId) {
        LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeachplanMedia::getTeachplanId,teachPlanId);
        wrapper.eq(TeachplanMedia::getMediaId,mediaId);
        teachplanMediaMapper.delete(wrapper);
    }


    /**
     * @description 获取最新的排序号
     * @param courseId 课程id
     * @param parentId 父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }
}
